package com.usooft.metaTalent.admin.system;

import com.usooft.framework.core.query.Page;
import com.usooft.framework.core.utils.MapBuilder;
import com.usooft.framework.web.auth.RequiresPermission;
import com.usooft.framework.web.auth.RequiresPermissionDependOn;
import com.usooft.metaTalent.admin.system.form.DictEditForm;
import com.usooft.metaTalent.admin.system.form.DictListForm;
import com.usooft.metaTalent.core.system.entity.Dict;
import com.usooft.metaTalent.core.system.service.DictService;
import com.usooft.metaTalent.core.system.wrapper.DictWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/system/dict")
@RequiredArgsConstructor
public class DictController {

    private final DictService dictService;

    @GetMapping("/list")
    @RequiresPermission(value = "/system/dict/list", description = "数据字典/查询")
    public ResponseEntity<?> list(DictListForm form) {
        Page<DictWrapper> page = dictService.page(form.buildQuery());
        return ResponseEntity.ok(page);
    }

    @PostMapping("/create")
    @RequiresPermission(value = "/system/dict/create", description = "数据字典/添加")
    public ResponseEntity<?> create(DictEditForm form) {
        dictService.create(form.getName(), form.getDescription());
        return ResponseEntity.ok(MapBuilder.builder().build());
    }

    @GetMapping("/create/get")
    @RequiresPermissionDependOn("/system/dict/create")
    public ResponseEntity<?> createGet() {
        return ResponseEntity.ok(MapBuilder.builder()
                .build());
    }

    @PostMapping("/edit")
    @RequiresPermission(value = "/system/dict/edit", description = "数据字典/修改")
    public ResponseEntity<?> edit(DictEditForm form) {
        Dict dict = dictService.get(form.getId());
        dictService.edit(dict, form.getName(), form.getDescription());
        return ResponseEntity.ok(MapBuilder.builder().build());
    }

    @GetMapping("/edit/get")
    @RequiresPermissionDependOn("/system/dict/edit")
    public ResponseEntity<?> editGet(Long id) {
        Dict dict = dictService.get(id);
        return ResponseEntity.ok(MapBuilder.builder()
                .put("source", dict)
                .build());
    }

}
